package com.jn.mvp.presenter;

import android.content.Context;

import com.google.gson.Gson;
import com.jn.mvp.application.MyApplication;
import com.jn.mvp.contract.RegionContract;
import com.jn.mvp.entity.RegionEntity;
import com.jn.mvp.http.HttpCallback;
import com.jn.mvp.http.HttpContract;
import com.jn.mvp.http.HttpHelper;
import com.jn.mvp.http.HttpRequest;
import com.jn.mvp.utils.LogUtils;

import java.util.List;

import jakarta.inject.Inject;

public class RegionPresenter implements RegionContract.IRegionPresenter, HttpCallback {
    private RegionContract.IRegionView mRegionView;
    private Context mContext;

    @Inject
    public Gson mGson;

    @Inject
    public HttpHelper mHttpHelper;

    public RegionPresenter(RegionContract.IRegionView mRegionView, Context mContext) {
        this.mRegionView = mRegionView;
        this.mContext = mContext;
        MyApplication.getInstance().getAllComponent().injectRegionPresenter(this);
    }

    @Override
    public void getData(String code) {
        mRegionView.onHttpLoading(true);
        HttpRequest request = new HttpRequest.Builder()
                .url(HttpContract.REGION_URL)
                .addParam("key", HttpContract.JUHE_KEY)
                .addParam("fid", code)
                .callBack(this)
                .build();
        mHttpHelper.postRequest(request);
    }

    @Override
    public void onHttpSuccess(String result, String url) {
        mRegionView.onHttpLoading(false);
        LogUtils.d("asdf", "onHttpSuccess : " + result);
        switch (url) {
            case HttpContract.REGION_URL:
                RegionEntity regionEntity = mGson.fromJson(result, RegionEntity.class);
                List<RegionEntity.ResultDTO> resultList = regionEntity.getResult();
                mRegionView.showData(resultList);
                break;
        }
    }

    @Override
    public void onHttpFail(String url) {
        mRegionView.onHttpLoading(false);
        mRegionView.onHttpFail();
    }

    @Override
    public void onHttpTimeout(String url) {
        mRegionView.onHttpLoading(false);
        mRegionView.onHttpTimeout();
    }

    @Override
    public void onHttpTokenExpiration(String result, String url) {
        mRegionView.onHttpLoading(false);
        mRegionView.onHttpTokenExpiration();
    }

    @Override
    public void onHttpError(String result, String url, String code) {
        mRegionView.onHttpLoading(false);
        mRegionView.onHttpFail(result);
    }
}